//
//  WebLoginViewController.m
//  Weibo
//
//  Created by wpapple on 16/6/1.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "WebLoginViewController.h"
#import "Account.h"
#import "MainTabBarController.h"

@interface WebLoginViewController ()<UIWebViewDelegate>

@end

@implementation WebLoginViewController

-(void)loadView{
    self.view = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    NSString * string = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",AppKey,RedirectURI];
    NSURL * url = [NSURL URLWithString:string];
    NSLog(@"%@",string);
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    UIWebView * webView = (UIWebView *)self.view;
    
    [webView loadRequest:request];
    webView.delegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString * url = request.URL.absoluteString;
    NSLog(@"%@",url);
    NSString * string = [NSString stringWithFormat:@"%@/?code=",RedirectURI];
    NSRange range = [url rangeOfString:string];
    if (range.location != NSNotFound) {
        NSInteger location = range.location + range.length;
        NSString * code = [url substringFromIndex:location];
        
        [self accessTokenWithCode:code];
        
        return NO;
        
    }
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
//    MainTabBarController * mainTabBarController = [[MainTabBarController alloc]init];
    
//    [UIApplication sharedApplication].keyWindow.rootViewController = mainTabBarController;
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    
}

-(void)accessTokenWithCode:(NSString *)code{
    
    NSMutableDictionary * parameters = [ NSMutableDictionary dictionary];
    parameters[@"client_id"] = AppKey;
    parameters[@"client_secret"] = AppSecret;
    parameters[@"redirect_uri"] = RedirectURI;
    parameters[@"grant_type"] = @"authorization_code";
    parameters[@"code"] = code;
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"access_token success");
        Account * account = [[Account alloc]init];
        account.access_token = responseObject[@"access_token"];
        account.uid = responseObject[@"uid"];
        
        [account save];
        
        [Account saveLoginStatus];
        
        NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"access_token faile");
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
